package com.sto.stoapp.db.manager;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.sto.stoapp.db.StoDataBase;
import com.sto.stoapp.db.constant.StoBaseConstant;
import com.sto.stoapp.db.constant.UserInfoConstant;
import com.sto.stoapp.http.response.download.info.UserInfoInfo;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.util.UnifiedStaticType;

/**
 * 用户基本信息管理类
 * 
 * @author cjb
 * 
 */
public class UserInfoManager extends StoDataBase implements
		DownLoadDao<UserInfoInfo> {
	static UserInfoManager mgr;

	private UserInfoManager(Context context) {
		super(context);

	}

	public static UserInfoManager getInstance(Context c) {
		if (mgr == null)
			mgr = new UserInfoManager(c);
		return mgr;
	}

	public boolean insert(UserInfoInfo entity) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			try {
				ContentValues values = new ContentValues();
				values.put(UserInfoConstant.KEY, entity.key);
				values.put(UserInfoConstant.VALUE, entity.value);
				values.put(UserInfoConstant.TYPE, entity.type);
				values.put(UserInfoConstant.BACKUP1, entity.backup1);
				values.put(UserInfoConstant.VERSION, entity.version);
				values.put(UserInfoConstant.ID, entity.id);
				values.put(STATUS, entity.status);
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE)
						.insert(USER_INFO_TABLE, null, values);
				if (res > 0) {
					Log.d("插入一条用户基本信息", "成功");
				} else {
					Log.d("插入一条用户基本信息", "失败");
				}
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("insert", e.getMessage());
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
			return res > 0 ? true : false;
		}
	}

	@Override
	public String getMaxVersion() {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			String version = "0";
			Cursor c = null;
			try {
				SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.READ_DATABASE);
				c = db.rawQuery(getMaxVersionSql(USER_INFO_TABLE), null);
				if (c != null && c.moveToNext()) {
					version = c.getString(c
							.getColumnIndex(StoBaseConstant.VERSION));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				closeCursor(c);
				closeDb(UnifiedStaticType.READ_DATABASE);
			}
			return version;
		}
	}

	@Override
	public boolean replace(ContentValues values) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long result = 0;
			try {
				SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
				result = db.replace(USER_INFO_TABLE, null, values);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result > 0;
		}
	}

	@Override
	public void deleteDataByStatus(String status) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {

			try {
				SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
				db.delete(USER_INFO_TABLE, STATUS + "=?",
						new String[] { status });
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}

		}
	}

	@Override
	public void deleteAll() {

		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			try {
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE)
						.delete(USER_INFO_TABLE, null, null);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("deleteAll", e.getMessage());
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}

		}

	}

	@Override
	public ContentValues buildContentValues(UserInfoInfo entity) {
		ContentValues values = new ContentValues();
		values.put(UserInfoConstant.KEY, entity.key);
		values.put(UserInfoConstant.VALUE, entity.value);
		values.put(UserInfoConstant.TYPE, entity.type);
		values.put(UserInfoConstant.BACKUP1, entity.backup1);
		values.put(UserInfoConstant.VERSION, entity.version);
		values.put(UserInfoConstant.ID, entity.id);
		values.put(STATUS, entity.status);
		return values;
	}

	/**
	 * 插入多数据
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void insertOrReplace(List<UserInfoInfo> datas) {

		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
			try {
				db.beginTransaction();
				for (UserInfoInfo info : datas) {
					long res = -1;
					ContentValues values = buildContentValues(info);
					// res = db.insert(USER_INFO_TABLE, null, values);
					// if (res > 0) {
					// Log.d("插入一条用户基本信息", "成功");
					// } else {
					if (replace(values)) {
						Log.d("插入一条用户基本信息", "成功");
					} else {
						Log.d("插入一条用户基本信息", "失败");
						// }
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("insertList", e.getMessage());
			} finally {
				datas.clear();
				db.setTransactionSuccessful();
				db.endTransaction();
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
		}
	}

	/**
	 * 离线登录 判断
	 * 
	 * @param site
	 * @param jobNumber
	 * @param passWord
	 * @return
	 */
	public boolean hasThisUser(String site, String jobNumber, String passWord) {
		SQLiteDatabase qb = getSqliteDatabase(UnifiedStaticType.READ_DATABASE);
		String sql = "select count(*) from " + USER_INFO_TABLE + " where "
				+ UserInfoConstant.KEY + "=? and " + UserInfoConstant.VALUE
				+ "=?";
		Cursor cursor = qb.rawQuery(sql, new String[] { site + jobNumber,
				passWord });
		int count = 0;
		if (cursor != null && cursor.moveToFirst()) {
			count = cursor.getInt(0);
		}
		closeCursor(cursor);
		closeDb(UnifiedStaticType.READ_DATABASE);
		return count > 0;
	}

	public String getJobNameByCode(String site, String jobNumber) {
		SQLiteDatabase qb = getSqliteDatabase(UnifiedStaticType.READ_DATABASE);
		String sql = "select " + UserInfoConstant.BACKUP1 + " from "
				+ USER_INFO_TABLE + " where " + UserInfoConstant.KEY + "=? ";
		Cursor cursor = qb.rawQuery(sql, new String[] { site + jobNumber });
		String name = "";
		try {
			if (cursor != null && cursor.moveToFirst()) {
				name = cursor.getString(cursor
						.getColumnIndex(UserInfoConstant.BACKUP1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		closeCursor(cursor);
		closeDb(UnifiedStaticType.READ_DATABASE);
		return name;
	}
}
